<?php

namespace App\Http\Controllers\pc\banner;

use App\Http\Controllers\common\LMReturn;
use App\Http\Controllers\Controller;
use App\Models\Banner;
use App\Http\Resources\Banner as ResBanner;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class BannerController extends Controller
{
    /**
     * @description: 增加banner
     * @param {Request} $request
     * @return {*}
     */
    public function addBanner(Request $request)
    {

        // 接参
        try {
            //去验证token值
            $tokenInfo = json_decode($this->verifyTokenLm(), true);
            //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
            if ($tokenInfo['code'] == 400) {
                return LMReturn::tokenErr($tokenInfo['msg'], $tokenInfo['code']);
            }
            $all = $request->all();
            $title = $all['title'] ?? '';
            $content = $all['content'] ?? '';
            $url = $all['url'] ?? '';
            $file = $all['file'] ?? false;
            $img_url = $all['img_url'] ?? '';
            $jump_type = $all['jump_type'] ?? 1;
            $weight = $all['weight'] ?? 100;
            $type = $all['type'] ?? 1;

            if (!$file && $img_url == '') {
                return LMReturn::Error("请上传图片", 'IC007');
            } else {
                // 获取上传文件的后缀
                $ext = $file->getClientOriginalExtension();
                $filename = md5(uniqid()) . "." . $ext;
                $realPath = $file->getRealPath();
                $path = 'image_hx/' . date("Ymd") . '/' . $filename; // 路径
                $exists = Storage::disk('new_ftp')->put('htdocs/' . $path, file_get_contents($realPath));
                if ($exists) {
                    $img_url = env("FTP2_URL") . $path;
                } else {
                    return LMReturn::Error('图片上传错误', 'BCAB2');
                }
            }
        } catch (\Throwable $th) {
            return LMReturn::Error("参数错误", 'BCAB3');
        }


        // 赋值
        try {
            $banner = new Banner();
            $banner->title = $title;
            $banner->content = $content;
            $banner->img_url = $img_url;
            $banner->url = $url;
            $banner->jump_type = $jump_type;
            $banner->weight = $weight;
            $banner->type = $type;
            $banner->img_path = $path;
            $banner->save();
            return LMReturn::Success($banner);
        } catch (\Throwable $th) {
            return LMReturn::Error("入库时发生问题", 'BCAB1');
        }
    }
    /**
     * @description: 查询banner
     * @param {Request} $request
     * @return {*}
     */
    public function queryBanner(Request $request)
    {
        // 接参
        try {
            //去验证token值
            $tokenInfo = json_decode($this->verifyTokenLm(), true);
            //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
            if ($tokenInfo['code'] == 400) {
                return LMReturn::tokenErr($tokenInfo['msg'], $tokenInfo['code']);
            }
            $all = $request->all();
            $size = $all['size'] ?? 15;
            $type = $all['type'] ?? '';
            $jump_type = $all['jump_type'] ?? '';
        } catch (\Throwable $th) {
            return LMReturn::Error("接参时发生问题", 'BCQB01');
        }

        try {
            $where = [];
            if ($type != '') {
                $where[] =  ['type', $type];
            }
            if ($jump_type != '') {
                $where[] =  ['jump_type', $jump_type];
            }
            $banner_list = Banner::where($where)->paginate($size);
            if ($banner_list) {
                $banner_lists = ResBanner::collection($banner_list);
                $data['data'] = $banner_lists;
                $data['current_page'] = $banner_list['current_page'];
                $data['total'] = $banner_list['total'];
                return LMReturn::Success($data);
            }
            return LMReturn::Success();
        } catch (\Throwable $th) {
            return LMReturn::Error("查询时发生问题", 'BCQB02');
        }
    }
    /**
     * @description: 删除banner
     * @param {Request} $request
     * @return {*}
     */
    public function deleteBanner(Request $request)
    {
        // 接参
        try {
            //去验证token值
            $tokenInfo = json_decode($this->verifyTokenLm(), true);
            //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
            if ($tokenInfo['code'] == 400) {
                return LMReturn::tokenErr($tokenInfo['msg'], $tokenInfo['code']);
            }
            $all = $request->all();
            $id = $all['id'];
        } catch (\Throwable $th) {
            return LMReturn::Error("接参时发生问题", 'BCQB01');
        }

        try {
            $banner = Banner::find($id);
            // 1 删除 文件图片
            $exists = Storage::disk('new_ftp')->delete('htdocs/' . $banner->img_path);
            // 2 删除 数据库图片
            if ($exists) {
                $res = $banner->delete();
                return LMReturn::Success($res);
            } else {
                return LMReturn::Error('删除失败，原因可能是  找不到 banner图片');
            }
        } catch (\Throwable $th) {
            return LMReturn::Error("删除时发生问题", 'BCQB02');
        }
    }
    /**
     * @description: 修改banner
     * @param {Request} $request
     * @return {*}
     */
    public function changeBanner(Request $request)
    {
        // 接参
        try {
            //去验证token值
            $tokenInfo = json_decode($this->verifyTokenLm(), true);
            //判断传递过来的token的code码是否等于400  如果是400的话，提示token不正确
            if ($tokenInfo['code'] == 400) {
                return LMReturn::tokenErr($tokenInfo['msg'], $tokenInfo['code']);
            }

            $all = $request->all();
            $id = $all['id'];
            $banner =  Banner::find($id);
            $title = $all['title'] ?? $banner->title;
            $content = $all['content'] ?? $banner->content;
            $url = $all['url'] ?? $banner->url;
            $file = $all['file'] ?? false;
            $img_url = $all['img_url'] ?? '';
            $jump_type = $all['jump_type'] ?? $banner->jump_type;
            $weight = $all['weight'] ?? $banner->weight;
            $type = $all['type'] ?? $banner->type;

            if ($file) {
                // 获取上传文件的后缀
                $ext = $file->getClientOriginalExtension();
                $filename = md5(uniqid()) . "." . $ext;
                $realPath = $file->getRealPath();
                $path = 'image_hx/' . date("Ymd") . '/' . $filename; // 路径
                $exists = Storage::disk('new_ftp')->put('htdocs/' . $path, file_get_contents($realPath));
                if ($exists) {
                    $img_url = env("FTP2_URL") . $path;
                } else {
                    return LMReturn::Error('图片上传错误', 'BCCB01');
                }
            } else {
                $path = $banner->img_path;
            }
        } catch (\Throwable $th) {
            return LMReturn::Error("参数错误", 'BCCB02');
        }

        // 赋值
        try {
            $banner->title = $title;
            $banner->content = $content;
            if ($img_url != '') {
                $banner->img_url = $img_url;
                $banner->img_path = $path ?? '';
            }
            $banner->url = $url;
            $banner->jump_type = $jump_type;
            $banner->weight = $weight;
            $banner->type = $type;
            $banner->save();
            return LMReturn::Success($banner);
        } catch (\Throwable $th) {
            return LMReturn::Error("入库时发生问题", 'BCCB1');
        }
    }
}
